package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.model.Task;
import ge.telasi.tasks.model.TaskRelation;
import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * @author dimitri
 */
public class TaskRelationPanel extends JPanel {
    private TaskRelationEditPanel relationPanel;
    private boolean parent;
    private JLabel label;
    private Task task;

    public TaskRelationPanel() {
        initComponents();
    }

    private void initComponents() {
        // set layout and outer border
        setLayout(new BorderLayout(5, 5));
        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        // add description label
        label = new JLabel("...");
        add(label, BorderLayout.NORTH);

        // add relation panel
        relationPanel = new TaskRelationEditPanel();
        add(relationPanel, BorderLayout.CENTER);

        // relation panel
        relationPanel.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                if ("task".equals(evt.getPropertyName())) {
                    firePropertyChange("task", evt.getOldValue(), evt.getNewValue());
                }
            }
        });
    }

    public List<TaskRelation> getRelations() {
        return relationPanel.getRelations(parent);
    }

    public void setRelations(List<TaskRelation> relations, boolean parent) {
        setParent(parent);
        relationPanel.setRelations(relations, parent);
    }

    public boolean isParent() {
        return parent;
    }

    public void setParent(boolean parent) {
        this.parent = parent;
        if (parent) {
            label.setText("დავალებები, რომლებიც მოცემულ დავალებას უდევს საფუძვლად:");
            label.setIcon(TaskDecontationUtils.getParentIcon());
        } else {
            label.setText("დავალებები, რომლებიც წარმოადგენს ამ დავალების ქვედავალებებს:");
            label.setIcon(TaskDecontationUtils.getChildIcon());
        }
    }

    public Task getTask() {
        return task;
    }

    public void setTask(Task task, boolean parent) {
        this.task = task;
        this.parent = parent;
        setRelations(parent ? task.getParents() : task.getChildren(), parent);
        relationPanel.setTask(task);
    }

    public void setToolbarVisible(boolean visible) {
        relationPanel.setToolbarVisible(visible);
    }

}
